package main

import (
	"fmt"
	"os"
	"runtime"

	"gitee.com/realjf/wob/tools/bctl/api/apigen"
	"gitee.com/realjf/wob/tools/bctl/dao/daogen"
	"gitee.com/realjf/wob/tools/bctl/internal/version"
	"gitee.com/realjf/wob/tools/bctl/service/svcgen"
	"github.com/logrusorgru/aurora"
	"github.com/urfave/cli"
	"github.com/zeromicro/go-zero/tools/goctl/completion"
)

const codeFailure = 1

var commands = []cli.Command{
	{
		Name:  "api",
		Usage: "generate api related files",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "o",
				Usage: "the output api file",
			},
		},
		Action: apigen.ApiCommand,
	},
	{
		Name:  "dao",
		Usage: "generate dao related files",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "o",
				Usage: "the output dao file",
			},
		},
		Action: daogen.DaoCommand,
	},
	{
		Name:  "svc",
		Usage: "generate service related files",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "o",
				Usage: "the output service file",
			},
		},
		Action: svcgen.DaoCommand,
	},
}

func main() {
	cli.BashCompletionFlag = cli.BoolFlag{
		Name:   completion.BashCompletionFlag,
		Hidden: true,
	}

	app := cli.NewApp()
	app.EnableBashCompletion = true
	app.Usage = "a cli tool to generate code"
	app.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH)
	app.Commands = commands

	if err := app.Run(os.Args); err != nil {
		fmt.Println(aurora.Red(err.Error()))
		os.Exit(codeFailure)
	}
}
